﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Xml.Linq;
using CuteSprite.Eme.Configuration;
using CuteSprite.Eme.Data;
using CuteSprite.Eme.Data.CRM;
using CuteSprite.Eme.Server.Configuration.Resources;
using CuteSprite.Eme.UI.Utils;
using HmFramework;
using HmFramework.Components;
using HmFramework.IO;
using HmFramework.UI.Utils;
using HmFramework.Xml;
using ResManagerUtils = CuteSprite.Eme.UI.Utils.Resources.ResManager;
using CuteSprite.Eme.Data.Sprite;

namespace CuteSprite.Eme.Server.Configuration
{
	public partial class FrmMain : HmBaseForm
	{
		private AdminConfigInfo m_settings;
		private XElement m_elRoot;
		private XElement m_elConns;

		#region -- 界面初始化 --

		public FrmMain()
		{
			InitializeComponent();
			InitForm();
			InitTheme();
		}

		#endregion

		#region -- 窗体事件 --

		/// <summary>载入窗体图标</summary>
		protected override void InitIcon()
		{
			this.Icon = ResManagerUtils.GetIcon("logo.ico");
		}

		/// <summary>载入语言包</summary>
		protected override void LoadLocalization()
		{
			this.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_From);
			btnOK.Text = UICommonLocalizer.GetString(UICommonStringId.OKButtonText);
			btnCancel.Text = UICommonLocalizer.GetString(UICommonStringId.CancelButtonText);
			tbcConfig.Tabs["tbIPAllow"].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_tbIPAllow);
			lvwIP.Columns[0].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lvwIPColumnStartIP);
			lvwIP.Columns[1].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lvwIPColumnEndIP);
			btnIPAdd.Text = UICommonLocalizer.GetString(UICommonStringId.AddButtonText);
			btnIPEdit.Text = UICommonLocalizer.GetString(UICommonStringId.EditButtonText);
			btnIPDelete.Text = UICommonLocalizer.GetString(UICommonStringId.DeleteButtonText);
			tbcConfig.Tabs["tbUsers"].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_tbUsers);
			lvwUsers.Columns[0].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lvwIPColumnUser);
			btnUserAdd.Text = UICommonLocalizer.GetString(UICommonStringId.AddButtonText);
			btnUserEdit.Text = UICommonLocalizer.GetString(UICommonStringId.EditButtonText);
			btnUserDelete.Text = UICommonLocalizer.GetString(UICommonStringId.DeleteButtonText);
			tbcConfig.Tabs["tbConn"].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_tbConn);
			lvwConn.Columns[0].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lvwConnColumnName);
			lvwConn.Columns[1].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lvwConnColumnType);
			lvwConn.Columns[2].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lvwConnColumnDefault);
			btnConnAdd.Text = UICommonLocalizer.GetString(UICommonStringId.AddButtonTextWithoutKey);
			btnConnEdit.Text = UICommonLocalizer.GetString(UICommonStringId.EditButtonText);
			btnConnDelete.Text = UICommonLocalizer.GetString(UICommonStringId.DeleteButtonText);
			btnConnDelete.Text = UICommonLocalizer.GetString(UICommonStringId.DeleteButtonText);
			btnDefault.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lvwConnColumnDefault);
			tbcConfig.Tabs["tbSettings"].Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_tbSettings);
			lblDataPath.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lblDataPath);
			lblMailPath.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lblMailPath);
			lblImPath.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lblImPath);
			lblAttachPath.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lblAttachPath);
			lblPort.Text = EmeConfigLocalizer.GetString(EmeConfigStringId.FrmMain_Caption_lblPort);
			//EmeConfigLocalizer.Active.WriteToXml(@"c:\aaa.xml");
		}

		public override void FormOnLoad()
		{
			LoadSettings();
		}

		#endregion

		#region -- 加载/保存 --

		#region - method LoadSettings -

		/// <summary>载入配置信息</summary>
		private void LoadSettings()
		{
			m_settings = AdminConfigs.GetConfig();

			LoadIPAccessItems();
			LoadUserItems();
			LoadConns();

			lvwIP_SelectedIndexChanged(this, null);
			lvwUsers_SelectedIndexChanged(this, null);
			lvwConn_SelectedIndexChanged(this, null);

			var config = GeneralConfigs.GetConfig();
			txtDataPath.Text = config.DataPath;
			txtMailPath.Text = config.MailStorePath;
			txtImPath.Text = config.IMStorePath;
			txtAttachPath.Text = config.AttachStorePath;
			txtPort.Value = config.ManagementServerPort;
		}

		#endregion

		#region - method LoadIPAccessItems -

		private void LoadIPAccessItems()
		{
			var ipRows = m_settings.GetTableRows(AdminConfigInfo._.Table_IPAccess);
			lvwIP.Items.Clear();
			foreach (DataRow dr in ipRows)
			{
				ListViewItem it = new ListViewItem(dr[AdminConfigInfo._.Column_IPAccess_StartIP].ToString());
				it.SubItems.Add(dr[AdminConfigInfo._.Column_IPAccess_EndIP].ToString());
				it.Tag = dr;
				lvwIP.Items.Add(it);
			}
		}

		#endregion

		#region - method LoadUserItems -

		private void LoadUserItems()
		{
			var userRows = m_settings.GetTableRows(AdminConfigInfo._.Table_User);
			lvwUsers.Items.Clear();
			foreach (DataRow dr in userRows)
			{
				ListViewItem it = new ListViewItem(dr[AdminConfigInfo._.Column_User_Name].ToString());
				it.Tag = dr;
				lvwUsers.Items.Add(it);
			}
			lvwIP_SelectedIndexChanged(this, null);
			lvwUsers_SelectedIndexChanged(this, null);
		}

		#endregion

		#region - method LoadConns -

		private void LoadConns()
		{
			var file = FileHelper.FileExists(PathHelper.ApplicationStartupPathCombine("Config", "DbProvider.config"));
			if (!MatchHelper.StrIsNullOrEmpty(file))
			{
				var xodc = XDocument.Load(file);
				m_elRoot = xodc.Element("configuration");
				if (m_elRoot != null)
				{
					m_elConns = m_elRoot.Element("DataConnections");
					foreach (XElement item in m_elConns.Elements())
					{
						var listItem = new ListViewItem(item.Attribute("name").Value);
						var providerName = item.Attribute("provider").Value.ToLowerInvariant();
						if (providerName.Contains("mssql") ||
								providerName.Contains("system.data.sqlclient") ||
								providerName.Contains("sql2012") ||
								providerName.Contains("sql2008") ||
								providerName.Contains("sql2005") ||
								providerName.Contains("sql2000") ||
								providerName == "sqlclient")
						{
							listItem.SubItems.Add("SQL Server");
						}
						else if (providerName.Contains("mysql.data.mysqlclient")
									|| providerName.Contains("mysql"))
						{
							listItem.SubItems.Add("MySQL");
						}
						else if (providerName.Contains("oracle")
									|| providerName.Contains("oracleclient"))
						{
							listItem.SubItems.Add("Oracle");
						}
						else if (providerName.Contains("postgresql")
									|| providerName.Contains("npgsql")
									|| providerName.Contains("postgresql.data.postgresqlclient"))
						{
							listItem.SubItems.Add("PostgreSQL");
						}
						else if (providerName.Contains("firebird")
									|| providerName.Contains("firebirdclient")
									|| providerName.Contains("firebirdsql.data.firebirdclient"))
						{
							listItem.SubItems.Add("Firebird");
						}
						else if (providerName.Contains("sqlite"))
						{
							listItem.SubItems.Add("SQLite");
						}
						else if (providerName.Contains("sqlce"))
						{
							listItem.SubItems.Add("SQL Server CE");
						}
						if (item.Attribute("defaulted").Value.EqualIgnoreCase("true"))
						{
							listItem.SubItems.Add("Y");
						}
						else
						{
							listItem.SubItems.Add("N");
						}
						listItem.Tag = item;
						lvwConn.Items.Add(listItem);
					}
				}
			}
		}

		#endregion

		#region - method SaveSettings -

		/// <summary>保存配置信息</summary>
		public void SaveSettings()
		{
			try
			{
				if (m_settings.HasChanges())
				{
					AdminConfigs.SaveConfig();
				}
				if (m_elRoot != null)
				{
					using (var writer = new HmXmlWriterX(true))
					{
						writer.Open(PathHelper.ApplicationStartupPathCombine("Config", "DbProvider.config"));
						m_elRoot.WriteTo(writer.InnerWriter);
					}
				}
				var config = GeneralConfigs.GetConfig();
				config.DataPath = txtDataPath.Text.Trim();
				config.MailStorePath = txtMailPath.Text.Trim();
				config.IMStorePath = txtImPath.Text.Trim();
				config.AttachStorePath = txtAttachPath.Text.Trim();
				config.ManagementServerPort = txtPort.Value;
				GeneralConfigs.SaveConfig();
			}
			catch (Exception ex)
			{
				ProcessException(ex);
			}
		}

		#endregion

		#endregion

		#region -- Events Handling --

		#region - method btnCancel_Click -

		private void btnCancel_Click(Object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion

		#region - method btnOK_Click -

		private void btnOK_Click(Object sender, EventArgs e)
		{
			SaveSettings();
			DialogResult = DialogResult.OK;
			this.Close();
		}

		#endregion

		#region - method lvwIP_DoubleClick -

		private void lvwIP_DoubleClick(Object sender, EventArgs e)
		{
			if (lvwIP.SelectedItems.Count == 0) { return; }
			btnIPEdit_Click(this, null);
		}

		#endregion

		#region - method lvwIP_SelectedIndexChanged -

		private void lvwIP_SelectedIndexChanged(Object sender, EventArgs e)
		{
			if (lvwIP.SelectedItems.Count == 0)
			{
				btnIPEdit.Enabled = false;
				btnIPDelete.Enabled = false;
			}
			else
			{
				btnIPEdit.Enabled = true;
				btnIPDelete.Enabled = true;
			}
		}

		#endregion

		#region - method lvwUsers_DoubleClick -

		private void lvwUsers_DoubleClick(Object sender, EventArgs e)
		{
			if (lvwUsers.SelectedItems.Count == 0) { return; }
			btnUserEdit_Click(this, null);
		}

		#endregion

		#region - method lvwUsers_SelectedIndexChanged -

		private void lvwUsers_SelectedIndexChanged(Object sender, EventArgs e)
		{
			if (lvwUsers.SelectedItems.Count == 0)
			{
				btnUserEdit.Enabled = false;
				btnUserDelete.Enabled = false;
			}
			else
			{
				btnUserEdit.Enabled = true;
				btnUserDelete.Enabled = true;
			}
		}

		#endregion

		#region - method btnUserAdd_Click --

		private void btnUserAdd_Click(Object sender, EventArgs e)
		{
			var frm = FrmUser.Create(m_settings);
			if (frm.ShowDialog(this) == DialogResult.OK)
			{
				LoadUserItems();
			}
		}

		#endregion

		#region - method btnUserEdit_Click -

		private void btnUserEdit_Click(Object sender, EventArgs e)
		{
			var item = lvwUsers.SelectedItems[0];
			var frm = FrmUser.Create(m_settings, (DataRow)item.Tag);
			if (frm.ShowDialog(this) == DialogResult.OK)
			{
			}
		}

		#endregion

		#region - method btnUserDelete_Click -

		private void btnUserDelete_Click(Object sender, EventArgs e)
		{
			var item = lvwUsers.SelectedItems[0];
			if (item.Text.ToLower() == "administrator")
			{
				DialogHelper.ShowWarning(UICommonLocalizer.GetString(UICommonStringId.ShowWarningDialogTitle),
						EmeConfigLocalizer.GetString(EmeConfigStringId.Message_DelAdmin));
				return;
			}
			if (DialogHelper.AskQuestion(UICommonLocalizer.GetString(UICommonStringId.ShowConfirmDialogTitle),
						EmeConfigLocalizer.GetString(EmeConfigStringId.Message_DelUserConfirm).FormatWith(item.Text)) == eTaskDialogResult.Yes)
			{
				((DataRow)item.Tag).Delete();
				item.Remove();
			}
		}

		#endregion

		#region - method btnIPAdd_Click -

		private void btnIPAdd_Click(Object sender, EventArgs e)
		{
			var frm = FrmIPAccess.Create(m_settings);
			if (frm.ShowDialog(this) == DialogResult.OK)
			{
				LoadIPAccessItems();
			}
		}

		#endregion

		#region - method btnIPEdit_Click -

		private void btnIPEdit_Click(Object sender, EventArgs e)
		{
			var item = lvwIP.SelectedItems[0];
			if (item.Text.ToLower() == "127.0.0.1" && item.SubItems[0].Text == "127.0.0.1")
			{
				DialogHelper.ShowWarning(this.Text, EmeConfigLocalizer.GetString(EmeConfigStringId.Message_EditLocalIP));
				return;
			}
			var frm = FrmIPAccess.Create(m_settings, (DataRow)item.Tag);
			if (frm.ShowDialog(this) == DialogResult.OK)
			{
				LoadIPAccessItems();
			}
		}

		#endregion

		#region - method btnIPDelete_Click -

		private void btnIPDelete_Click(Object sender, EventArgs e)
		{
			var item = lvwIP.SelectedItems[0];
			if (item.Text.ToLower() == "127.0.0.1")
			{
				DialogHelper.ShowWarning(UICommonLocalizer.GetString(UICommonStringId.ShowWarningDialogTitle),
						EmeConfigLocalizer.GetString(EmeConfigStringId.Message_DelLocalIP));
				return;
			}
			if (DialogHelper.AskQuestion(UICommonLocalizer.GetString(UICommonStringId.ShowConfirmDialogTitle),
						EmeConfigLocalizer.GetString(EmeConfigStringId.Message_DelIPConfirm).FormatWith(item.Text)) == eTaskDialogResult.Yes)
			{
				((DataRow)item.Tag).Delete();
				item.Remove();
			}
		}

		#endregion

		#region - method lvwConn_DoubleClick -

		private void lvwConn_DoubleClick(object sender, EventArgs e)
		{
			if (lvwConn.SelectedItems.Count == 0) { return; }
			btnConnEdit_Click(this, null);
		}

		#endregion

		#region - method lvwConn_SelectedIndexChanged -

		private void lvwConn_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lvwConn.SelectedItems.Count == 0)
			{
				btnConnEdit.Enabled = false;
				btnConnDelete.Enabled = false;
				btnDefault.Enabled = false;
			}
			else
			{
				btnConnEdit.Enabled = true;
				btnConnDelete.Enabled = true;
				btnDefault.Enabled = true;
			}
		}

		#endregion

		#region - method btiMsSql_Click -

		private void btiMsSql_Click(object sender, EventArgs e)
		{
			var frmConn = FrmMsSqlConn.Create();
			if (frmConn.ShowDialog(this) == DialogResult.OK)
			{
				var el = frmConn.ConnElement;
				m_elConns.Add(el);
				var listItem = new ListViewItem(el.Attribute("name").Value);
				listItem.SubItems.Add("SQL Server");
				listItem.SubItems.Add("N");
				listItem.Tag = el;
				lvwConn.Items.Add(listItem);
			}
		}

		#endregion

		#region - method btiMySql_Click -

		private void btiMySql_Click(object sender, EventArgs e)
		{
			var frmConn = FrmMySqlConn.Create();
			if (frmConn.ShowDialog(this) == DialogResult.OK)
			{
				var el = frmConn.ConnElement;
				m_elConns.Add(el);
				var listItem = new ListViewItem(el.Attribute("name").Value);
				listItem.SubItems.Add("MySQL");
				listItem.SubItems.Add("N");
				listItem.Tag = el;
				lvwConn.Items.Add(listItem);
			}
		}

		#endregion

		#region - method btiPostgreSQL_Click -

		private void btiPostgreSQL_Click(object sender, EventArgs e)
		{
			var frmConn = FrmPostgreSQLConn.Create();
			if (frmConn.ShowDialog(this) == DialogResult.OK)
			{
				var el = frmConn.ConnElement;
				m_elConns.Add(el);
				var listItem = new ListViewItem(el.Attribute("name").Value);
				listItem.SubItems.Add("PostgreSQL");
				listItem.SubItems.Add("N");
				listItem.Tag = el;
				lvwConn.Items.Add(listItem);
			}
		}

		#endregion

		private void btiOracle_Click(object sender, EventArgs e)
		{
			//Oracle
		}

		private void btiFirebird_Click(object sender, EventArgs e)
		{
			//Firebird
		}

		#region - method btiSqlite_Click -

		private void btiSqlite_Click(object sender, EventArgs e)
		{
			var frmConn = FrmSqliteConn.Create();
			if (frmConn.ShowDialog(this) == DialogResult.OK)
			{
				var el = frmConn.ConnElement;
				m_elConns.Add(el);
				var listItem = new ListViewItem(el.Attribute("name").Value);
				listItem.SubItems.Add("SQLite");
				listItem.SubItems.Add("N");
				listItem.Tag = el;
				lvwConn.Items.Add(listItem);
			}
		}

		#endregion

		#region - method btiSqlCe_Click -

		private void btiSqlCe_Click(object sender, EventArgs e)
		{
			var frmConn = FrmSqlCeConn.Create();
			if (frmConn.ShowDialog(this) == DialogResult.OK)
			{
				var el = frmConn.ConnElement;
				m_elConns.Add(el);
				var listItem = new ListViewItem(el.Attribute("name").Value);
				listItem.SubItems.Add("SQL Server CE");
				listItem.SubItems.Add("N");
				listItem.Tag = el;
				lvwConn.Items.Add(listItem);
			}
		}

		#endregion

		#region - method btnConnEdit_Click -

		private void btnConnEdit_Click(object sender, EventArgs e)
		{
			var item = lvwConn.SelectedItems[0];
			var type = item.SubItems[1].Text;
			var listItem = lvwConn.SelectedItems[0];

			switch (type)
			{
				case "SQL Server":
					var frmMsSql = FrmMsSqlConn.Create((XElement)item.Tag);
					if (frmMsSql.ShowDialog(this) == DialogResult.OK)
					{
						var el = frmMsSql.ConnElement;
						listItem.SubItems[0].Text = el.Attribute("name").Value;
					}
					break;

				case "MySQL":
					var frmMySql = FrmMySqlConn.Create((XElement)item.Tag);
					if (frmMySql.ShowDialog(this) == DialogResult.OK)
					{
						var el = frmMySql.ConnElement;
						listItem.SubItems[0].Text = el.Attribute("name").Value;
					}
					break;

				case "Oracle":
					break;

				case "PostgreSQL":
					var frmPgSql = FrmPostgreSQLConn.Create((XElement)item.Tag);
					if (frmPgSql.ShowDialog(this) == DialogResult.OK)
					{
						var el = frmPgSql.ConnElement;
						listItem.SubItems[0].Text = el.Attribute("name").Value;
					}
					break;

				case "Firebird":
					break;

				case "SQLite":
					var frmSqlite = FrmSqliteConn.Create((XElement)item.Tag);
					if (frmSqlite.ShowDialog(this) == DialogResult.OK)
					{
						var el = frmSqlite.ConnElement;
						listItem.SubItems[0].Text = el.Attribute("name").Value;
					}
					break;

				case "SQL Server CE":
					var frmSqlCe = FrmSqlCeConn.Create((XElement)item.Tag);
					if (frmSqlCe.ShowDialog(this) == DialogResult.OK)
					{
						var el = frmSqlCe.ConnElement;
						listItem.SubItems[0].Text = el.Attribute("name").Value;
					}
					break;

				default:
					break;
			}
		}

		#endregion

		#region - method btnConnDelete_Click -

		private void btnConnDelete_Click(object sender, EventArgs e)
		{
			var item = lvwConn.SelectedItems[0];
			((XElement)item.Tag).Remove();
			item.Remove();
		}

		#endregion

		#region - method btnDefault_Click -

		private void btnDefault_Click(object sender, EventArgs e)
		{
			foreach (ListViewItem item in lvwConn.Items)
			{
				item.SubItems[2].Text = "N";
			}
			foreach (XElement el in m_elConns.Elements())
			{
				el.Attribute("defaulted").Value = "false";
			}
			var listItem = lvwConn.SelectedItems[0];
			listItem.SubItems[2].Text = "Y";
			((XElement)listItem.Tag).Attribute("defaulted").Value = "true";
		}

		#endregion

		private void txtDataPath_ButtonCustomClick(object sender, EventArgs e)
		{
			var folderDlg = new FolderBrowserDialog();
			folderDlg.ShowDialog();
			txtDataPath.Text = folderDlg.SelectedPath;
		}

		private void txtMailPath_ButtonCustomClick(object sender, EventArgs e)
		{
			var folderDlg = new FolderBrowserDialog();
			folderDlg.ShowDialog();
			txtMailPath.Text = folderDlg.SelectedPath;
		}

		private void txtImPath_ButtonCustomClick(object sender, EventArgs e)
		{
			var folderDlg = new FolderBrowserDialog();
			folderDlg.ShowDialog();
			txtImPath.Text = folderDlg.SelectedPath;
		}

		private void txtAttachPath_ButtonCustomClick(object sender, EventArgs e)
		{
			var folderDlg = new FolderBrowserDialog();
			folderDlg.ShowDialog();
			txtAttachPath.Text = folderDlg.SelectedPath;
		}

		#endregion

		#region -- 其他 --

		private void btnInitData_Click(object sender, EventArgs e)
		{
			try
			{
				if (Area.Meta.Count > 0)
				{
					DialogHelper.ShowMessage("导入地区", eTaskDialogIcon.Information2, "地区数据已经初始化！", null, eTaskDialogButton.Ok);
				}
				else
				{
					Area.Import();
					DialogHelper.ShowMessage("导入地区", eTaskDialogIcon.Information2, "地区数据导入成功！", null, eTaskDialogButton.Ok);
				}
			}
			catch { }
		}

		private void btnInitCountry_Click(object sender, EventArgs e)
		{
			try
			{
				if (Country.Meta.Count > 0)
				{
					DialogHelper.ShowMessage("导入国家", eTaskDialogIcon.Information2, "国家数据已经初始化！", null, eTaskDialogButton.Ok);
				}
				else
				{
					Country.Import();
					DialogHelper.ShowMessage("导入国家", eTaskDialogIcon.Information2, "国家数据导入成功！", null, eTaskDialogButton.Ok);
				}
			}
			catch { }
		}

		private void btnInitPermissionItem_Click(object sender, EventArgs e)
		{
			try
			{
				//if (PermissionItem.Meta.Count > 0)
				//{
				//	DialogHelper.ShowMessage("导入操作权限项", eTaskDialogIcon.Information2, "操作权限项数据已经初始化！", null, eTaskDialogButton.Ok);
				//}
				//else
				//{
				//	PermissionItem.ImportXml(ResManager.GetFileResource("PermissionItem.xml"));
				//	DialogHelper.ShowMessage("导入操作权限项", eTaskDialogIcon.Information2, "操作权限项数据导入成功！", null, eTaskDialogButton.Ok);
				//}
				if (ManageProvider.Provider.PermissionItemFactory.Count > 0)
				{
					DialogHelper.ShowMessage("导入操作权限项", eTaskDialogIcon.Information2, "操作权限项数据已经初始化！", null, eTaskDialogButton.Ok);
				}
				else
				{
					var admin = ManageProvider.Provider.FindByID(1);
					admin.ImportPermissionItemXml(ResManager.GetFileResource("PermissionItem.xml"));
					DialogHelper.ShowMessage("导入操作权限项", eTaskDialogIcon.Information2, "操作权限项数据导入成功！", null, eTaskDialogButton.Ok);
				}
			}
			catch { }
		}

		private void btnInitMenu_Click(object sender, EventArgs e)
		{
			try
			{
				//if (EmeMenu.Meta.Count > 0)
				//{
				//	DialogHelper.ShowMessage("导入菜单", eTaskDialogIcon.Information2, "菜单数据已经初始化！", null, eTaskDialogButton.Ok);
				//}
				//else
				//{
				//	EmeMenu.ImportXml(ResManager.GetFileResource("Menu.xml"));
				//	DialogHelper.ShowMessage("导入菜单", eTaskDialogIcon.Information2, "菜单数据导入成功！", null, eTaskDialogButton.Ok);
				//}
				if (ManageProvider.Provider.MenuFactory.Count > 0)
				{
					DialogHelper.ShowMessage("导入菜单", eTaskDialogIcon.Information2, "菜单数据已经初始化！", null, eTaskDialogButton.Ok);
				}
				else
				{
					var admin = ManageProvider.Provider.FindByID(1);
					admin.ImportMenuXml(ResManager.GetFileResource("Menu.xml"));
					DialogHelper.ShowMessage("导入菜单", eTaskDialogIcon.Information2, "菜单数据导入成功！", null, eTaskDialogButton.Ok);
				}
			}
			catch { }
		}

		private void btnImportEntity_Click(object sender, EventArgs e)
		{
			try
			{
#if DEBUG
				DataModel.ImportEntity();
				//DataModel.ImportXml(@"d:\EmeCRM.xml");
				DialogHelper.ShowMessage("导入实体模型", eTaskDialogIcon.Information2, "导入成功！", null, eTaskDialogButton.Ok);
#else
#endif
			}
			catch (Exception ex)
			{
				ProcessException(ex);
			}
		}

		private void btnOrganizeDataTransform_Click(object sender, EventArgs e)
		{
			var frm = new FrmOrganizeDataTransform();
			frm.ShowDialog(this);
			frm.Close();
		}

		private void btnImportExpressMisSendsAndRecs_Click(object sender, EventArgs e)
		{
			try
			{
				//ExpressMix.ImportExpressMixSendsAndRecipients();
			}
			catch (Exception ex)
			{
				ProcessException(ex);
			}
		}

		private void btnCompanyDiscount_Click(object sender, EventArgs e)
		{
			var frm = new FrmCompanyDiscount();
			frm.ShowDialog(this);
			frm.Close();
		}

		#endregion
	}
}
